import request from '@/utils/request'
import request2 from '@/utils/request/index2.js'
import Iot from '@/utils/request/index3.js'
import StorageUtils from "../../utils/StorageUtils";
// api地址
const api = {
  propert: 'device/get/propert',
  historyPropert: 'device/get/history/propert/otherKey',
  saveDevice: 'device/save/productId/deviceName',
  getDeviceInfo: 'device/info/deviceName/product',
  getMqttParam: 'device/mqtt/param/product',
  getProduceBluName: 'product/bluetooth/all',
  getProduce: 'product/one',
  propertlast: 'device/data/last',
  statistics: 'device/history/statistics/day',
  sceneType: 'scene/type/all',
  scenenotify: 'scene/notify',
  scene: 'scene',
  deviceSet: 'device/set/property',
  bindDevice: 'device/user/xmyk',
  getBindDevice: 'device/user',
  hrvReport: 'device/data/hrvReport',
  service: 'device/service/param',
  sceneDel: 'scene/',
	statisticsShu: 'userData/statistic',
}

// 通过设备ID获取redis属性信息
export function getPropert(data) {
  return Iot.get(api.propert, data,{header: {
        'content-type': 'application/x-www-form-urlencoded',
    }})
}
//通过设备ID和时间和唯一标识查看属性
export function historyPropert(data) {
	// console.log(112312321321);
	// console.log(data);
  return Iot.get(api.historyPropert, {...data,identifiers:data.identifier,otherKey:{key1:'patient:'+StorageUtils.get('uuid')}},{load:data.load?true:false,header: {
        'content-type': 'application/x-www-form-urlencoded',
    }})
}
//用户最后一条
export function propertLast(data) {
  return request2.get(api.propertlast, {uuid:StorageUtils.get('uuid')},{header: {
        'content-type': 'application/x-www-form-urlencoded',
    }})
}
// 保存设备
export function saveDevice(data) {
    return Iot.post(api.saveDevice, data, {header: {
        'content-type': 'application/x-www-form-urlencoded',
    }})
}
// 获取设备信息
export function getDeviceInfo(data) {
    return Iot.get(api.getDeviceInfo, data, {header: {
            'content-type': 'application/x-www-form-urlencoded',
        }})
}
// 获取mqtt信息
export function getMqttParam(data) {
    return Iot.get(api.getMqttParam, data)
}
export function getdeviceSet(data) {
    return Iot.get(api.deviceSet, data, {header: {
            'content-type': 'application/x-www-form-urlencoded',
        }})
}

// 获取所有产品的蓝牙名称
export const getProduceBluName = () => {
    return Iot.get(api.getProduceBluName, data, {header: {
            'content-type': 'application/x-www-form-urlencoded',
        }})
}
export const getSceneType = () => {
    return request2.get(api.sceneType, {}, {header: {
            'content-type': 'application/x-www-form-urlencoded',
        }})
}
export const postScenenotify = (data) => {
    return request2.post(api.scenenotify, data, {header: {
            'content-type': 'application/json',
        }})
}
export const putScene = (data) => {
    return request2.put(api.scene, data, {header: {
            'content-type': 'application/json',
        }})
}
export const getScenenotify = (data) => {
    return request2.get(api.scenenotify, data, {header: {
            'content-type': 'application/x-www-form-urlencoded',
        }})
}
export const delScene = (data) => {
    return request2.delete(api.sceneDel, data, {header: {
            'content-type': 'application/x-www-form-urlencoded',
        }})
}

export const hrvReport = (data) => {
    return request2.get(api.hrvReport, data, {header: {
            'content-type': 'application/x-www-form-urlencoded',
        }})
}

// 获取产品信息 只需要传其中的一个参数
export function getProduce({productKey, bluetoothName}) {
    let param = {};
    if(!!bluetoothName) param.bluetoothName = bluetoothName;
    if (!!productKey) param.productKey = productKey;
    return Iot.get(api.getProduce, param, {header: {
            'content-type': 'application/x-www-form-urlencoded',
        }})
}
export function getStatistics(data) {
   return request2.get(api.statisticsShu, {
   	...data,
   	dataTypes: data.identifier,
   	groupBy: data.isMonthly ? 'month' : 'day',
   	uuid: StorageUtils.get('uuid')
   }, {
   	header: {
   		'content-type': 'application/x-www-form-urlencoded',
   	}
   })
}
// 绑定设备
export function bindDevice(data, option = {}) {
	const options = {
		isPrompt: true, //（默认 true 说明：本接口抛出的错误是否提示）
		load: true, //（默认 true 说明：本接口是否提示加载动画）
		...option
	}
	return request2.put(api.bindDevice, data, options);
}

// 获取用户绑定的设备
export function getBindDevice(data) {
	return request2.get(api.getBindDevice, data);
}

// 服务调用
export function invokeService(data) {
	return Iot.post(api.service, JSON.stringify(data), {header:{
		'content-type': "application/json",
	}});
}

// 检查用户是否绑定该设备
export function checkBindDevice({
	deviceName,
	deviceType,
	uuid
}) {
	const param = {
		deviceName,
		deviceType,
		uuid,
	};
	return request2.get(api.checkBindDevice, param, {
		load: false,
		isPrompt: false
	});
}
